<?php

namespace app\index\model;

use think\Model;
use think\model\concern\SoftDelete;

class PhotoList extends Model
{
    use SoftDelete;
    protected $name = 'car_photo_list'; //表名
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $dateFormat = 'Y/m/d H:i:s';
    protected $createTime = 'create_at';
    protected $updateTime = 'update_at';
    protected $deleteTime='delete_at';
    public function tanName()
    {
        return $this->hasOne(PohtoType::class, 'id', 'type_id')->bind(['tabName' => 'name']);
    }

    public function release()
    {
//        ['email', 'user_type', 'nickname', 'avatar', 'rank']
        return $this->hasOne(UserModel::class, 'id', 'releaseid')->bind([
            'releaseName' => 'nickname',
            'releaseAvatar' => 'avatar',
        ]);
    }

    public function imgHeight()
    {
        return $this->hasOne(PohtoListImage::class, 'image_url', 'cover')->bind(['height' => 'height']);
    }
    public function userInfo() {
        return $this->hasOne(PohtoUser::class, 'id', 'author_id')->with(['institutionName'])->bind([
            'institution_name',
            'institution_id',
            'institution_avatar_id',
            'author_name' => 'username',
            'avatar_id',
            'avatar',
            'institution_avatar'
        ]);
    }

    public function child()
    {
        return $this->hasMany(PohtoListImage::class, 'photo_id', 'id')->order('sort', 'desc')->order('create_at', 'asc');
    }

    public function author()
    {
        return $this->hasOne(PohtoUser::class, 'id', 'author_id');

    }

    public function coll()
    {
        return $this->hasOne(PhotoColl::class, 'photo_id', 'id');
    }

    public function like()
    {
        return $this->hasOne(PhotoLike::class, 'photo_id', 'id');
    }

    public function report()
    {
        return $this->hasOne(PohtoReport::class, 'photo_id', 'id');
    }

    public function buy()
    {
        return $this->hasOne(PhotoBuy::class, 'photo_id', 'id');
    }

    public function comment()
    {
        return $this->hasOne(CommentModel::class, 'exchangeId', 'id')->where('type', 4);
    }

    public function getPrivateAttr($value)
    {
        return $value == 1;
    }
    public function setTagsAttr($value)
    {
        if($value) {
            return implode(",",$value);
        }
        return null;
    }

    public function getTagsAttr($value)
    {
        if($value) {
            return explode(',',$value);
        }
        return [];
    }
}